package com.xiaoxu.maxTemp;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.partition.HashPartitioner;

import java.io.IOException;

/**
 * hadoop -jar **.jar
 * com.xiaoxu.maxTemp.MaxTemperature
 * inputParh
 * outputPath
 */

public class MaxTemperature {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        if (args.length != 2) {
            System.err.println("Usage: MaxTemperature <input path> <output path>");
            System.exit(-1);
        }
        Job job = new Job();
        //反射机制
        job.setJarByClass(MaxTemperature.class);
        //工作名称
        job.setJobName("Max temperature");

        //设置输入路径
        FileInputFormat.addInputPath(job, new Path(args[0]));
        //设置输出路径
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        //默认设置
        //job.setPartitionerClass(HashPartitioner.class);
        //job.setNumReduceTasks(1);

        job.setPartitionerClass(MaxTemperaturePartitioner.class);
        job.setNumReduceTasks(4);



        job.setMapperClass(MaxTemperatureMapper.class);
        //combiner优化 减少网络开销，节省内存，硬盘
        job.setCombinerClass(MaxTemperatureReducer.class);
        job.setReducerClass(MaxTemperatureReducer.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);


        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}
